#install.packages("tidyverse")
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0     ✔ purrr   1.0.1
## ✔ tibble  3.1.8     ✔ dplyr   1.1.0
## ✔ tidyr   1.3.0     ✔ stringr 1.5.0
## ✔ readr   2.1.3     ✔ forcats 1.0.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
#install.packages("devtools")
library(devtools)
## Loading required package: usethis
#devtools::install_github("rstudio-education/dsbox", force = TRUE)
library(dsbox)
#install.packages("ggridges")
library(ggridges)
#install.packages("gganimate")
library(gganimate)
## No renderer backend detected. gganimate will default to writing frames to separate files
## Consider installing:
## - the `gifski` package for gif output
## - the `av` package for video output
## and restarting the R session
#install.packages("janitor")
library(janitor)
## 
## Attaching package: 'janitor'
## 
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
#install.packages("stringr")
library(stringr)
#install.packages("plotly")
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
cod_landings <- read_csv("data/cod-landings.csv")
## Rows: 1196 Columns: 10
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): State, NMFS Name, Collection, Scientific Name, Source
## dbl (2): Year, Tsn
## num (3): Pounds, Metric Tons, Dollars
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
cod_landings <- cod_landings 
  colnames(cod_landings)[3] = "Common_Name"
  colnames(cod_landings)[5] = "Metric_Tons"
  colnames(cod_landings)[6] = "US_Dollars"
  colnames(cod_landings)[7] = "Fishing_Types"
  colnames(cod_landings)[8] = "Scientific_Name"
  colnames(cod_landings)[9] = "Taxonomic_Serial_Number"
  colnames(cod_landings)[10] = "Fishing_Companies"
glimpse(cod_landings)
## Rows: 1,196
## Columns: 10
## $ Year                    <dbl> 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021…
## $ State                   <chr> "ALASKA", "CONNECTICUT", "CONNECTICUT", "DELAW…
## $ Common_Name             <chr> "COD, PACIFIC", "COD, ATLANTIC", "COD, ATLANTI…
## $ Pounds                  <dbl> 330404171, 2277, 7921, 18, 1171, 47311, 120113…
## $ Metric_Tons             <dbl> 149870, 1, 4, 0, 1, 21, 545, 166, 21, 21, 41, …
## $ US_Dollars              <dbl> 116766003, 5031, NA, NA, NA, 128833, 2619925, …
## $ Fishing_Types           <chr> "Commercial", "Commercial", "Recreational", "R…
## $ Scientific_Name         <chr> "Gadus macrocephalus", "Gadus morhua", NA, NA,…
## $ Taxonomic_Serial_Number <dbl> 164711, 164712, 164712, 164712, 164712, 164712…
## $ Fishing_Companies       <chr> "AKFIN", "ACCSP", "MRIP", "MRIP", "MRIP", "ACC…
cod_landings$Common_Name <- str_replace(cod_landings$Common_Name,"COD, PACIFIC", "Pacific_Cod")

cod_landings$Common_Name <- str_replace(cod_landings$Common_Name,"COD, ATLANTIC", "Atlantic_Cod")

cod_landings$Scientific_Name <- str_replace(cod_landings$Scientific_Name, "Gadus macrocephalus", "Gadus_macrocephalus")

cod_landings$Scientific_Name <- str_replace(cod_landings$Scientific_Name, "Gadus morhua", "Gadus_morhua")
cod_landings
## # A tibble: 1,196 × 10
##     Year State    Commo…¹ Pounds Metri…² US_Do…³ Fishi…⁴ Scien…⁵ Taxon…⁶ Fishi…⁷
##    <dbl> <chr>    <chr>    <dbl>   <dbl>   <dbl> <chr>   <chr>     <dbl> <chr>  
##  1  2021 ALASKA   Pacifi… 3.30e8  149870  1.17e8 Commer… Gadus_…  164711 AKFIN  
##  2  2021 CONNECT… Atlant… 2.28e3       1  5.03e3 Commer… Gadus_…  164712 ACCSP  
##  3  2021 CONNECT… Atlant… 7.92e3       4 NA      Recrea… <NA>     164712 MRIP   
##  4  2021 DELAWARE Atlant… 1.8 e1       0 NA      Recrea… <NA>     164712 MRIP   
##  5  2021 MAINE    Atlant… 1.17e3       1 NA      Recrea… <NA>     164712 MRIP   
##  6  2021 MAINE    Atlant… 4.73e4      21  1.29e5 Commer… Gadus_…  164712 ACCSP  
##  7  2021 MASSACH… Atlant… 1.20e6     545  2.62e6 Commer… Gadus_…  164712 ACCSP  
##  8  2021 MASSACH… Atlant… 3.65e5     166 NA      Recrea… <NA>     164712 MRIP   
##  9  2021 NEW HAM… Atlant… 4.63e4      21 NA      Recrea… <NA>     164712 MRIP   
## 10  2021 NEW HAM… Atlant… 4.58e4      21  1.25e5 Commer… Gadus_…  164712 ACCSP  
## # … with 1,186 more rows, and abbreviated variable names ¹​Common_Name,
## #   ²​Metric_Tons, ³​US_Dollars, ⁴​Fishing_Types, ⁵​Scientific_Name,
## #   ⁶​Taxonomic_Serial_Number, ⁷​Fishing_Companies

Q1 - How do the weights of recorded recreational and commercial Cod landings of the Atlantic and Pacific compare to one another between the years 1950 and 2021? Plan Q1 - Summarize the data through a ridgeline density plot of weight of landings, colored by “Fishing_Type” and faceted by “Common_Name”. Also include an animated plot of the same type to better show the fluctuation in landings every 10 years.

#plotly allows to get info on the gppah directly... still better moving
#exclude arctic and toothed cod?
#How to make it continuous like an actual area? these are discrete variables
# the years are not in order for the Pacific cod?
#how to see each year and change graduation for the pounds to make it easier?
#stacked bar chart better?
#add + customize labels
#Choose colors
# add cod moratorium in 1992?

Graph_01 <- cod_landings %>% 
  ggplot(mapping = aes(
         x = Year,
         y = Pounds,
         color = Common_Name)) +
    geom_area(alpha=0.5) +
    geom_line() +
    scale_x_continuous(breaks = seq(0, 100, 1))
#facet_wrap( ~ Common_Name)
# if want to choose color --> geom_area(fill="#69b3a2", alpha=0.5) +
  # geom_line(color="#69b3a2") 

Graph_01 <- ggplotly(Graph_01)
## Warning: Removed 34 rows containing non-finite values (`stat_align()`).
Graph_01
#the mutating is not working
#Density doesn't represent the pounds... find another plot
cod_landings %>% 
  mutate(Atlantic = if_else(Common_Name == "COD, ATLANTIC", 
                           "Atlantic", 
                           "Pacific" )) %>% 
  ggplot(mapping = aes(x = Year, 
                       fill = Fishing_Types, 
                       color = severity ))+
  geom_density_line(alpha = 0.5, 
               color = "black")+
  scale_x_continuous(breaks = seq(1950, 2021, 5))

#change graduation on the axis
#Add labs
#Change color
#Make it move